package com.bbs.gui.action.bbs;

import com.bbs.db.pers.Boardinfo;
import com.bbs.db.pers.Postinfo;
import com.bbs.db.pers.Replyinfo;
import com.bbs.db.pers.Userinfo;
import com.bbs.gui.form.BBoardForm;
import com.bbs.gui.form.BPostForm;
import com.bbs.gui.form.BReplyForm;
import com.bbs.gui.iface.BbsIface;
import com.bbs.logic.iface.BbsService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Iterator;
import java.util.List;
import org.apache.struts2.ServletActionContext;

/**
 * @time Nov 14, 2009, 7:50:45 AM
 * @author fdcube
 */
public class BbsAction extends ActionSupport implements BbsIface {

    private String returnUrl;

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }
    private BbsService bbsService;

    public void setBbsService(BbsService bbsService) {
        this.bbsService = bbsService;
    }
    private BPostForm bpostForm;

    public BPostForm getBpostForm() {
        return bpostForm;
    }

    public void setBpostForm(BPostForm bpostForm) {
        this.bpostForm = bpostForm;
    }
    public List<Boardinfo> bList;

    public String SAddPost() {
        try {
            bList = bbsService.GetBoards();
            System.out.println("长度：" + bList.size());
            return "shownpost";
        } catch (Exception e) {
            System.out.println(e);
            this.addActionError("服务器错误");
            return ERROR;
        }
    }

    public String AddPost() {
        ptmp = new Postinfo(new Boardinfo(bpostForm.getBid()), new Userinfo((Integer) ActionContext.getContext().getSession().get("uid")), ServletActionContext.getRequest().getRemoteAddr(), bpostForm.getPtitle(), bpostForm.getPcontent());
        System.out.println("content--" + ptmp.getPcontent() + "title" + ptmp.getPtitle() + "pip" + ptmp.getPip() + "bid" + ptmp.getBoardinfo().getBid() + "uid" + ptmp.getUserinfo().getUid());
        if (bbsService.Post(ptmp)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    private int pid;

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }
    public int edit = 0;

    public int getEdit() {
        return edit;
    }

    public void setEdit(int edit) {
        this.edit = edit;
    }

    public String EditPost() {
        if (edit == 0) {
            ptmp = bbsService.GetPost(new Postinfo(pid)).get(0);
            bpostForm = new BPostForm();
            bpostForm.setBid(ptmp.getBoardinfo().getBid());
            bpostForm.setPid(String.valueOf(pid));
            bpostForm.setPtitle(ptmp.getPtitle());
            bpostForm.setPcontent(ptmp.getPcontent());
            return "editp";
        } else {
            if (bbsService.EditPost(new Postinfo(Integer.parseInt(bpostForm.getPid()), new Boardinfo(bpostForm.getBid()), new Userinfo((Integer) ActionContext.getContext().getSession().get("uid")), ServletActionContext.getRequest().getRemoteAddr(), bpostForm.getPtitle(), bpostForm.getPcontent()))) {
                edit = 0;
                return SUCCESS;
            } else {
                return ERROR;
            }
        }
    }
    public List<Postinfo> pList;

    public List<Postinfo> getpList() {
        return pList;
    }

    public void setpList(List<Postinfo> pList) {
        this.pList = pList;
    }

    public String ListPosts() {
        try {
            pList = bbsService.GetPosts();
            return SUCCESS;
        } catch (Exception e) {
            System.out.println(e);
            return ERROR;
        }
    }

    public String ShowPosts() {
        try {
            ptmp = new Postinfo(new Userinfo((Integer) ActionContext.getContext().getSession().get("uid")));
            pList = bbsService.GetPost(ptmp);
            return "plist";
        } catch (Exception e) {
            return ERROR;
        }
    }

    public String DeletePost() {
        if (bbsService.DeletePost(new Postinfo(pid))) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    BBoardForm bboardForm;

    public BBoardForm getBboardForm() {
        return bboardForm;
    }

    public void setBboardForm(BBoardForm bboardForm) {
        this.bboardForm = bboardForm;
    }
    public int bid;

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public List<Boardinfo> getbList() {
        return bList;
    }

    public void setbList(List<Boardinfo> bList) {
        this.bList = bList;
    }

    public String ListBoard() {
        try {
            bList = bbsService.GetBoards();
            return SUCCESS;
        } catch (Exception e) {
            System.out.println(e);
            return ERROR;
        }
    }

    public String ShowBbs() {
        try {
            bList = bbsService.GetBoards();
            System.out.println(bList.get(0).getPostinfos().size());
            return "showbbs";
        } catch (Exception e) {
            System.out.println(e);
            return ERROR;
        }
    }
    public Postinfo ptmp;

    public Postinfo getPtmp() {
        return ptmp;
    }

    public void setPtmp(Postinfo ptmp) {
        this.ptmp = ptmp;
    }
    public List<Replyinfo> rtmp;

    public String ShowPost() {
        try {
            ptmp = bbsService.GetPost(new Postinfo(pid)).get(0);
            rtmp = bbsService.GetReplies(new Replyinfo(ptmp));
            return "showpost";
        } catch (Exception e) {
            return ERROR;
        }
    }
    private BReplyForm breplyForm;

    public BReplyForm getBreplyForm() {
        return breplyForm;
    }

    public void setBreplyForm(BReplyForm breplyForm) {
        this.breplyForm = breplyForm;
    }

    public String AddReply() {
        Replyinfo artmp;
        if (ActionContext.getContext().getSession().get("uid") != null) {
            artmp = new Replyinfo(new Postinfo(Integer.parseInt(breplyForm.getPid())), new Userinfo((Integer) ActionContext.getContext().getSession().get("uid")), ServletActionContext.getRequest().getRemoteAddr(), breplyForm.getRinfo());
        } else {
            artmp = new Replyinfo(new Postinfo(Integer.parseInt(breplyForm.getPid())), new Userinfo(breplyForm.getUid()), ServletActionContext.getRequest().getRemoteAddr(), breplyForm.getRinfo());
        }
        if (bbsService.Reply(artmp)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
}
